package com.xiexin.web.controller.orderset;

import com.xiexin.common.annotation.Log;
import com.xiexin.common.core.controller.BaseController;
import com.xiexin.common.core.domain.AjaxResult;
import com.xiexin.common.core.page.TableDataInfo;
import com.xiexin.common.enums.BusinessType;
import com.xiexin.common.utils.poi.ExcelUtil;
import com.xiexin.orderset.domain.Ordersetting;
import com.xiexin.orderset.service.IOrdersettingService;
import com.xiexin.web.controller.tool.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 预约设置Controller
 *
 * @author xieixin
 * @date 2022-03-25
 */
@RestController
@RequestMapping("/orderset/orderset")
public class OrdersettingController extends BaseController {
    @Autowired
    private IOrdersettingService ordersettingService;
    //根据月份查询对应的预约设置数据
    @PreAuthorize("@ss.hasPermi('ordersetting:ordersetting:list')")
    @GetMapping("/getOrderDate7Days")
    public AjaxResult getOrderDate7Days(){
        return AjaxResult.success(ordersettingService.getOrderDate7Days());
    }
    /**
     * 查询预约设置列表
     */
    @PreAuthorize("@ss.hasPermi('orderset:orderset:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ordersetting ordersetting) {
        startPage();
        List<Ordersetting> list = ordersettingService.selectOrdersettingList(ordersetting);
        return getDataTable(list);
    }

    /**
     * 导出预约设置列表
     */
    @PreAuthorize("@ss.hasPermi('orderset:orderset:export')")
    @Log(title = "预约设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ordersetting ordersetting) {
        List<Ordersetting> list = ordersettingService.selectOrdersettingList(ordersetting);
        ExcelUtil<Ordersetting> util = new ExcelUtil<Ordersetting>(Ordersetting.class);
        util.exportExcel(response, list, "预约设置数据");
    }

    /**
     * 获取预约设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('orderset:orderset:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ordersettingService.selectOrdersettingById(id));
    }

    /**
     * 新增预约设置
     */
    @PreAuthorize("@ss.hasPermi('orderset:orderset:add')")
    @Log(title = "预约设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ordersetting ordersetting) {
        return toAjax(ordersettingService.insertOrdersetting(ordersetting));
    }

    /**
     * 修改预约设置
     */
    @PreAuthorize("@ss.hasPermi('orderset:orderset:edit')")
    @Log(title = "预约设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ordersetting ordersetting) {
        return toAjax(ordersettingService.updateOrdersetting(ordersetting));
    }

    /**
     * 删除预约设置
     */
    @PreAuthorize("@ss.hasPermi('orderset:orderset:remove')")
    @Log(title = "预约设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ordersettingService.deleteOrdersettingByIds(ids));
    }

    // 导入 excel
    @RequestMapping(value = "/upExcel", method = {RequestMethod.POST})
    public AjaxResult upExcel(@RequestParam("excelFile") MultipartFile excelFile) throws IOException {
        System.out.println("excelFile = " + excelFile);
        //  对这个  excelFile 对象 进行  读取！ 获取里面的内容！！！
        List<String[]> rows = POIUtils.readExcel(excelFile);
        System.out.println("rows = " + rows);
        System.out.println("============================================ "  );
        // 遍历   N多行
        List<Ordersetting> ordersettingList = new ArrayList<>();
        for (int i = 0; i < rows.size(); i++) {
            Ordersetting o = new Ordersetting();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            String dateStr = rows.get(i)[0];
            String numStr=  rows.get(i)[1];
            try {
                Date date = sdf.parse(dateStr);
                o.setOrderDate(date);
                o.setNumber( Long.parseLong(numStr));
                ordersettingList.add(o);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("ordersettingList = " + ordersettingList);
        ordersettingService.add(ordersettingList);
        return AjaxResult.success();
    }

}
